package com.rykj.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rykj.AppProxy;
import com.rykj.R;
import com.rykj.base.refreshview.view.IRefreshListLoadViewFactory;

import androidx.annotation.DrawableRes;


/****
 * 自定义加载和显示数据加载失败view 需要绑定显示的view
 *
 * @author jery 2015年10月21日15:43:53
 */
public class RYEmptyView extends RelativeLayout implements IRefreshListLoadViewFactory.IEmptyView {
    private ImageView imgStatus;
    private TextView tvTips;
    private Button btnReload;
    private View bindView;// 被绑定的view
    private ProgressBar progressBar;

    private @DrawableRes
    int ERROR_IMAGE = AppProxy.EmptyConfig.ERROR_IMAGE;
    private @DrawableRes
    int EMPTY_IMAGE = AppProxy.EmptyConfig.EMPTY_IMAGE;

    public RYEmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        inflate(context, R.layout.layout_empty_view, this);
        //LayoutParams btnParma = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //btnParma.addRule(RelativeLayout.BELOW, R.id.emptyview_tv_tip);
        //btnParma.addRule(RelativeLayout.CENTER_HORIZONTAL);
        //btnParma.setMargins(0, 20, 0, 0);
        //
        //LayoutParams imgParma = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //imgParma.addRule(CENTER_IN_PARENT);
        //
        //LayoutParams progressBarParma = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //progressBarParma.addRule(CENTER_IN_PARENT);
        //
        //LayoutParams tvParma = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //tvParma.addRule(RelativeLayout.BELOW, R.id.emptyview_img);
        //tvParma.addRule(RelativeLayout.CENTER_HORIZONTAL);
        //tvParma.setMargins(0, 20, 0, 0);

        progressBar = findViewById(R.id.emptyview_progress);
        btnReload = findViewById(R.id.emptyview_btn);
        tvTips = findViewById(R.id.emptyview_tv_tip);
        imgStatus = findViewById(R.id.emptyview_img);

        //progressBar.setId(R.id.emptyview_progress);
        //btnReload.setId(R.id.emptyview_btn);
        //tvTips.setId(R.id.emptyview_tv_tip);
        //imgStatus.setId(R.id.emptyview_img);

        progressBar.setIndeterminate(true);
        progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.anim_loading));
        //progressBar.setLayoutParams(progressBarParma);

        btnReload.setText("刷新");
        //	btnReload.setTextColor(getResources().getColor(R.color.white));
        //  btnReload.setPadding(10, 10, 10, 10);
        //	btnReload.setBackgroundResource(R.drawable.button_selector_default);
        //  btnReload.setGravity(Gravity.CENTER);

        tvTips.setTextSize(18);
        // tvTips.setTextColor(getResources().getColor(R.color.gray_divider));
        // setMessage(R.string.emptyview_loading);
        // rotaAnimation = AnimationUtils.loadAnimation(context, R.anim.image_loading_anim);
        // imgStatus.setAnimation(rotaAnimation);
        // imgStatus.setImageResource(R.drawable.loading_comman);
        // imgStatus.startAnimation(rotaAnimation);

        //imgStatus.setLayoutParams(imgParma);

        imgStatus.setVisibility(View.INVISIBLE);
        btnReload.setVisibility(INVISIBLE);

        //addView(imgStatus, imgParma);
        //addView(progressBar, progressBarParma);
        //addView(tvTips, tvParma);
        //addView(btnReload, btnParma);
    }

    public RYEmptyView(Context context) {
        super(context);
        initView(context);
    }

    public void bind(View view) {
        this.bindView = view;
    }

    public void unbind() {
        this.bindView = null;
        setVisibility(INVISIBLE);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE) {
            if (bindView != null) {
                bindView.setVisibility(INVISIBLE);
            }
        } else {
            if (bindView != null) {
                bindView.setVisibility(VISIBLE);
            }
        }
    }

    public void showLoading(String loadingMsg) {
        showLoading();
    }

    public void showEmpty(String msg) {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
            btnReload.setVisibility(View.VISIBLE);
            imgStatus.setVisibility(View.VISIBLE);
            imgStatus.setImageResource(EMPTY_IMAGE);
            setMessage(msg);
        }
    }

    public void showLoadFail(String msg) {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
            btnReload.setVisibility(View.VISIBLE);
            imgStatus.setVisibility(View.VISIBLE);
            imgStatus.setImageResource(ERROR_IMAGE);
            setMessage(msg);
        }
    }

    public void loadFinish() {
        loadSuccuss();
    }

    public void loadSuccuss() {
        setVisibility(View.INVISIBLE);
    }

    @Override
    public void setReloadClickListener(OnClickListener clickListener) {
        btnReload.setOnClickListener(clickListener);
    }

    public void showLoading() {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.VISIBLE);
            btnReload.setVisibility(View.INVISIBLE);
            imgStatus.setVisibility(View.INVISIBLE);
            setMessage("加载中...");
        }
    }

    public void showEmpty() {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
            btnReload.setVisibility(View.VISIBLE);
            imgStatus.setVisibility(View.VISIBLE);
            imgStatus.setImageResource(EMPTY_IMAGE);
            setMessage("当前没有相关记录");
        }
    }

    public void showTips(String msg) {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
            btnReload.setVisibility(View.INVISIBLE);
            imgStatus.setVisibility(View.INVISIBLE);
            setMessage(msg);
        }
    }

    public void showEmptyNoBtn(String msg) {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
            btnReload.setVisibility(View.INVISIBLE);
            imgStatus.setImageResource(EMPTY_IMAGE);
            imgStatus.setVisibility(View.VISIBLE);
            setMessage(msg);
        }
    }

    public void showNetError() {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            btnReload.setVisibility(View.INVISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
            setMessage("加载失败，网络不给力");
            imgStatus.setVisibility(View.INVISIBLE);
            imgStatus.setImageResource(ERROR_IMAGE);
        }
    }

    public void showLoadFail() {
        if (bindView != null) {
            setVisibility(View.VISIBLE);
            progressBar.setVisibility(INVISIBLE);
            btnReload.setVisibility(View.VISIBLE);
            imgStatus.setVisibility(View.VISIBLE);
            imgStatus.setImageResource(ERROR_IMAGE);
            setMessage("加载失败");
        }
    }

    public void setERROR_IMAGE(int ERROR_IMAGE) {
        this.ERROR_IMAGE = ERROR_IMAGE;
    }

    public void setEMPTY_IMAGE(int EMPTY_IMAGE) {
        this.EMPTY_IMAGE = EMPTY_IMAGE;
    }

    public void setBtnReloadText(String text) {
        this.btnReload.setText(text);
    }

    public void setMessage(String message) {
        this.tvTips.setText(message);
    }

    public void setOnReloadListener(OnClickListener clickListener) {
        btnReload.setOnClickListener(clickListener);
    }

    public void setMessage(int stringId) {
        setMessage(getResources().getString(stringId));
    }

    public void shoLoadFail(String erroMsg) {
        showLoadFail(erroMsg);
    }

    public Button getBtnReload() {
        return btnReload;
    }
}
